﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;
using System.Activities.Presentation.Model;
using System.Activities;
using Microsoft.VisualBasic.Activities;
using System.Activities.Expressions;

namespace TransactionServer.ActivityDesign.Helpers
{
    class ComboBoxItemStringVariableConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            ModelItem modelItem = value as ModelItem;
            if (value != null)
            {

                if (modelItem.GetCurrentValue() is OutArgument<String>)
                {
                    OutArgument<String> outArgument = modelItem.GetCurrentValue() as OutArgument<String>;

                    if (outArgument != null)
                    {
                        Activity<Location<String>> expression = outArgument.Expression;
                        VisualBasicReference<String> vbexpression = expression as VisualBasicReference<String>;

                        if (vbexpression != null)
                        {
                            return vbexpression.ExpressionText;
                        }
                    }
                }
                else if (modelItem.GetCurrentValue() is InArgument<String>)
                {
                    InArgument<String> outArgument = modelItem.GetCurrentValue() as InArgument<String>;

                    if (outArgument != null)
                    {
                        Activity<String> expression = outArgument.Expression;
                        VisualBasicValue<String> vbexpression = expression as VisualBasicValue<String>;

                        if (vbexpression != null)
                        {
                            return vbexpression.ExpressionText;
                        }
                    }
                }

            }
            return null;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            string itemContent = (string)value;

            if (targetType == typeof(OutArgument<String>))
            {
                VisualBasicReference<String> vbArgument = new VisualBasicReference<String>(itemContent);
                return new OutArgument<String>(vbArgument);
            }
            else if (targetType == typeof(InArgument<String>))
            {
                VisualBasicValue<String> vbArgument = new VisualBasicValue<String>(itemContent);
                return new InArgument<String>(vbArgument);
            }

            return null;
        }
    }
}
